package com.atguigu.es.demo;

import com.atguigu.es.beans.Emp;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.AvgAggregation;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.List;

/**
 * Created by Smexy on 2021/7/9
 *
 *      使用对象封装查询的参数
 *
 *

 */
public class READDemo2 {

    public static void main(String[] args) throws IOException {

        //创建一个 JestClientFactory
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        // 从工厂中获取一个 客户端对象
        JestClient jestClient = jestClientFactory.getObject();

        //封装  match: {}
        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("hobby", "购物");

        //封装aggs:{}    AggregationBuilders工具类,封装出各种类型的aggs的参数请求体
        TermsAggregationBuilder aggs = AggregationBuilders.terms("gendercount")
                .field("gender.keyword")
                .size(2)
                .subAggregation(
                        AggregationBuilders.avg("avgage").field("age")
                );

        //封装 整个请求体 {}
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().aggregation(aggs).query(matchQueryBuilder);

        // 获取到字符串类型的请求体
        String s = searchSourceBuilder.toString();

        // 封装search对象
        Search search = new Search.Builder(s)
                .addIndex("test")
                .addType("emps")
                .build();

        // 对应 hits
        SearchResult searchResult = jestClient.execute(search);

        //遍历结果
        System.out.println(searchResult.getTotal());

        System.out.println(searchResult.getMaxScore());

        // 对应  hits.hits
        List<SearchResult.Hit<Emp, Void>> hits = searchResult.getHits(Emp.class);

        for (SearchResult.Hit<Emp, Void> hit : hits) {

            System.out.println("_index:"+hit.index);
            System.out.println("_type:"+hit.type);
            System.out.println("_id:"+hit.id);
            System.out.println("_score:"+hit.score);

            System.out.println(hit.source);

        }

        //获取 aggregations
        MetricAggregation aggregations = searchResult.getAggregations();


        TermsAggregation gendercount = aggregations.getTermsAggregation("gendercount");

        List<TermsAggregation.Entry> buckets = gendercount.getBuckets();

        for (TermsAggregation.Entry bucket : buckets) {

            System.out.println("key:"+bucket.getKey());
            System.out.println("doc_count:"+bucket.getCount());

            AvgAggregation avgage = bucket.getAvgAggregation("avgage");

            System.out.println("avgageValue:"+avgage.getAvg());

        }


        //关闭客户端
        jestClient.close();


    }
}
